/*
 * Gedigarr - Falling block game.
 * Copyright (C) 2010  BoredCorp
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package gedigarr.grid;

import gedigarr.game.GameConstants;
import gedigarr.objects.CharacterCell;
import gedigarr.timers.GameTimer;
import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;
import putil.Putil;

/**
 *
 * @author JC
 */
public class MenuGrid extends TextGrid {

  public static final int MOVE_UP = 1 << 0;
  public static final int MOVE_DOWN = 1 << 1;
  public static final int MOVE_LEFT = 1 << 2;
  public static final int MOVE_RIGHT = 1 << 3;
  public static final int ACTION_NEW_GAME = 0;
  public static final int ACTION_SCORES = 1;
  public static final int ACTION_OPTIONS = 2;
  public static final int ACTION_EXIT = 3;
  private MenuItem mniNew = new MenuItem( ACTION_NEW_GAME, 5, "NEW GAME", true );
  private MenuItem mniScores = new MenuItem( ACTION_SCORES, 7, "SCORES", true );
  private MenuItem mniOptions = new MenuItem( ACTION_OPTIONS, 9, "OPTION", false );
  private MenuItem mniExit = new MenuItem( ACTION_EXIT, 11, "EXIT", true );
  private ArrayList<MenuItem> menu = new ArrayList<MenuItem>();
  private int selectedItem = 0;
  public GameTimer tCursor = new GameTimer( 0L, 100L );
  private int textFlags = 0;
  private Color activeColour = Color.WHITE;
  private Color inactiveColour = Color.GRAY;
  //private CharacterCell[][] grid;// = new CharacterCell[][];
  private Point pos = new Point( GameConstants.GRID_WIDTH / 2, GameConstants.GRID_HEIGHT / 2 );

  public MenuGrid() {
    grid = new CharacterCell[ GameConstants.GRID_HEIGHT ][ GameConstants.GRID_WIDTH ];
    for ( int y = 0; y < GameConstants.GRID_HEIGHT; y++ ) {
      for ( int x = 0; x < GameConstants.GRID_WIDTH; x++ ) {
        grid[y][x] = new CharacterCell( false, false, new Color( 0, 170, 0 ), "images/trans.png" );
        //grid[y][x].setActive( Cell.INACTIVE );
      }
    }
  }

  private void addMenuItem( MenuItem item ) {
    menu.add( item );
  }

  private void removeMenuItem( MenuItem item ) {
    menu.remove( item );
    drawString( "          ", null, 4, item.row, GameConstants.GRID_WIDTH,
            1, FLAG_HORIZONTAL | FLAG_CENTER );
  }

  public void drawMenu() {
    Color colour = null;
    for ( MenuItem item : menu ) {
      colour = item.active ? activeColour : inactiveColour;
      drawString( item.text, colour, 4, item.row, GameConstants.GRID_WIDTH,
              5, FLAG_HORIZONTAL | FLAG_CENTER );
    }
  }

  /*public void setContinueAvailable( boolean b ) {
  if ( b ) {
  if ( !menu.contains( mniResume ) ) {
  addMenuItem( mniResume );
  }
  } else {
  if ( menu.contains( mniResume ) ) {
  removeMenuItem( mniResume );
  }
  }
  }
   */
  /*
  public void drawString( String text, int x, int y, int flags ) {
  int length = text.length();
  if ( Putil.isBitSet( flags, FLAG_CENTER ) ) {
  x -= Math.round( length / 2.0 );
  }
  
  for ( int i = 0; i < length; i++ ) {
  char c = text.charAt( i );
  
  if ( x < 0 || x >= GameConstants.GRID_WIDTH
  || y < 0 || y >= GameConstants.GRID_HEIGHT ) {
  if ( Putil.isBitSet( flags, FLAG_HORIZONTAL ) ) {
  x++;
  }
  if ( Putil.isBitSet( flags, FLAG_VERTICAL ) ) {
  y++;
  }
  continue;
  }
  
  grid[y][x].setChar( c );
  
  
  if ( Putil.isBitSet( flags, FLAG_HORIZONTAL ) ) {
  x++;
  }
  if ( Putil.isBitSet( flags, FLAG_VERTICAL ) ) {
  y++;
  }
  }
  }
   *
   */
//  private void setActive( int x, int y, boolean visible ) {
//    grid[y][x].setVisible( visible );
//  }
  private void setActive( boolean visible ) {
    for ( int x = 0; x < GameConstants.GRID_WIDTH; x++ ) {
      grid[menu.get( selectedItem ).row][x].setVisible( visible );
    }
  }

  public void setSelectedItem( int item ) {
    setActive( false );
    this.selectedItem = item;
    setActive( true );
  }

  public int getSelectedItem() {
    return this.selectedItem;
  }

  public void moveUp() {
    int temp = selectedItem;
    boolean isNextActive;
    do {
      temp = Putil.wrapIndex( this.menu.size(), temp - 1 );
      isNextActive = this.menu.get( temp ).active;
      if ( isNextActive ) {
        setSelectedItem( temp );
        break;
      }
    } while ( temp != selectedItem );
  }

  public void moveDown() {
    int temp = selectedItem;
    boolean isNextActive;
    do {
      temp = Putil.wrapIndex( this.menu.size(), temp + 1 );
      isNextActive = this.menu.get( temp ).active;
      if ( isNextActive ) {
        setSelectedItem( temp );
        break;
      }
    } while ( temp != selectedItem );
  }

  public void setup() {
    //addMenuItem( mniResume );
    addMenuItem( mniNew );
    addMenuItem( mniScores );
    addMenuItem( mniOptions );
    addMenuItem( mniExit );
    setSelectedItem( selectedItem );
    drawMenu();
    //  drawString( "New Game", 0, 0, FLAG_VERTICAL | FLAG_HORIZONTAL );
    //  drawString( "", 0, 0, FLAG_VERTICAL | FLAG_HORIZONTAL );
  }

  /*
  public void setCursor( int x, int y ) {
  setActive( pos.x, pos.y, false );
  pos.setLocation( x, y );
  setActive( x, y, true );
  }
   */

  class MenuItem {

    public int actionId;
    public int row;
    public String text;
    public boolean active;

    public MenuItem( int actionId, int row, String text, boolean active ) {
      this.actionId = actionId;
      this.row = row;
      this.text = text;
      this.active = active;
    }
  }
}
